﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using CourseManager.Model;
using CourseManager.Model.Data;
using Ext.Net;

namespace CourseManager.View.Contents.Student
{
	public partial class SelectCourseContent : Page
	{
		private static readonly object[] Data = new object[]
		                                        	{
		                                        		new object[] {"8:00-8:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"8:55-9:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"10:00-10:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"10:55-11:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"12:10-12:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"13:05-13:50", "", "", "", "", "", "", ""},
		                                        		new object[] {"14:10-14:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"15:05-15:55", "", "", "", "", "", "", ""},
		                                        		new object[] {"16:00-16:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"16:55-17:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"18:00-18:45", "", "", "", "", "", "", ""},
		                                        		new object[] {"18:55-19:40", "", "", "", "", "", "", ""},
		                                        		new object[] {"19:50-20:35", "", "", "", "", "", "", ""},
		                                        	};

		private Model.Student student;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (student == null)
			{
				if (Session["UserId"] == null)
				{
					Response.Redirect(Constants.ERROR_PAGE);
				}
				else
				{
					student = new Model.Student(Session["UserId"].ToString());
				}
			}
			if (!X.IsAjaxRequest)
			{
				UpdateSchedule();
			}
		}

		private void UpdateSchedule()
		{
			var buffer = new char[8*13];
			for (int i = 0; i < buffer.Length; ++i)
				buffer[i] = ' ';


			for (int i = 1; i <= 7; ++i)
			{
				for (int j = 0; j < 13; ++j)
				{
					((object[])Data[j])[i] = "";
				}
			}

			foreach (var course in student.CourseList)
			{
				foreach (var timeInfo in course.TimeInfoList)
				{
					for (var i = timeInfo.Start; i <= timeInfo.End; ++i)
					{
						((object[]) Data[i])[timeInfo.Day] = course.Tag;
						if (course.Tag != null)
						{
							buffer[timeInfo.Day*13 + i] = char.Parse(course.Tag.ToString());
						}
					}
				}
			}
			Store.DataSource = Data;
			Store.DataBind();
			SelectedCouseStore.DataSource = from course in student.CourseList
			                                select new
			                                       	{
			                                       		tag = course.Tag.ToString(),
			                                       		tId = course.TId,
			                                       		courseId = course.CourseId,
			                                       		credit = course.Credit,
			                                       		courseName = course.CourseName,
			                                       		teacherName = course.TeacherName,
			                                       		time = course.Time,
			                                       		dept = course.Department,
			                                       		campus = course.Campus,
			                                       		place = course.Place,
			                                       		capacity = course.Capacity
			                                       	};
			SelectedCouseStore.DataBind();
			txtSchedule.Text = new string(buffer);
		}

		[DirectMethod]
		public bool SelectCourse(string courseId, string tId)
		{
			var course = Course.get(courseId, tId);
			if (!student.HasCourseClashWith(course))
			{
				student.AddCourse(course);
				UpdateSchedule();
				return true;
			}
			else
			{
				return false;
			}
		}

		[DirectMethod]
		public bool DeleteCourse(string id, string tId)
		{
			var course = Course.get(id, tId);
			student.RemoveCourse(course);
			UpdateSchedule();
			return true;
		}

		protected void BtnSearchClick(object sender, DirectEventArgs e)
		{
			var keywords = txtKeywords.Text.Split(' ');

			var entity = new DBEntity();
			bool flag = true;

			IEnumerable<View_Course> query = new List<View_Course>();

			foreach (var keyword in keywords)
			{
				query = (from course in flag ? entity.View_Course : query
				         where
				         	course.courseName.Contains(keyword) || course.courseId.Contains(keyword)
				         	|| course.teacherName.Contains(keyword) || course.time.Contains(keyword)
				         	|| course.tId.Contains(keyword) || course.campus.Contains(keyword)
				         	|| course.place.Contains(keyword) || course.capacity.ToString().Equals(keyword)
				         select course).ToList();
				flag = false;
			}

			CourseStore.DataSource = from result in query
			                         select new
			                                	{
			                                		tId = result.tId,
			                                		courseId = result.courseId,
			                                		credit = result.credit,
			                                		courseName = result.courseName,
			                                		teacherName = result.teacherName,
			                                		time = result.time,
			                                		dept = result.dept,
			                                		campus = result.campus,
			                                		place = result.place,
			                                		capacity = result.capacity
			                                	};

			pagingBar.SetPageIndex(1);
			CourseStore.DataBind();
		}
	}
}